package com.arcbbs.utils;

import com.arcbbs.config.AppConfig;
import com.arcbbs.config.WebConfig;
import com.arcbbs.constant.BaseConstant;
import com.arcbbs.entity.FileUploadDto;
import com.arcbbs.enums.DateTimePatternEnum;
import com.arcbbs.enums.FileUploadTypeEnum;
import com.arcbbs.exception.BaseException;
import com.sun.imageio.plugins.common.ImageUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;

@Component
public class FileUtils {
    @Resource
    private WebConfig webConfig;
    @Qualifier("appConfig")
    @Autowired
    private AppConfig appConfig;

    public FileUploadDto uploadDto(MultipartFile file, FileUploadTypeEnum fileUploadTypeEnum) {

        try {
            FileUploadDto dto = new FileUploadDto();
            String originalFilename = file.getOriginalFilename();
            String fileSuffix = StringTools.getFileSuffix(originalFilename);
            System.out.println(fileSuffix);
            if (originalFilename.length() > 200) {
                originalFilename = StringTools.getFileName(originalFilename).substring(0, 200) + fileSuffix;
            }
            if (!ArrayUtils.contains(fileUploadTypeEnum.getSuffixArray(), fileSuffix)) {
                throw new BaseException("类型不正确");

            }
            String month = DateUtil.format(new Date(), DateTimePatternEnum.YYYY_MM_DD.getPattern());
            String baseFolder = webConfig.getProjectFolder() + BaseConstant.FILE_FOLDER_FILE;
            File targetFileFolder = new File(baseFolder  + month + "/");

            String fileName = StringTools.getFinalString(15) + fileSuffix;
            System.out.println(fileName+"fileName");
            File targetFile = new File(targetFileFolder.getPath() + "/" + fileName);
            String localpath = month + "/" + fileName;
            if (fileUploadTypeEnum == FileUploadTypeEnum.AVATAR) {
                targetFileFolder=new File(baseFolder+BaseConstant.FILE_FOLDER_AVATAR_NAME);

                targetFile=new File(targetFileFolder.getPath()+"/"+fileName);
                System.out.println(fileName+"fileName");
                localpath="avatar"+"/"+fileName;


            }
            if (!targetFileFolder.exists()) {
                targetFileFolder.mkdir();

            }
            file.transferTo(targetFile);

//            //压缩图片
//            if(fileUploadTypeEnum==FileUploadTypeEnum.COMMENT_IMAGE){
//                String thumbnailName=targetFile.getName().replace(".","_.");
//                File thumbnail=new File(targetFile.getParent()+"/"+thumbnailName);
//                Boolean thumbnailCreateed=  ScaleFilter.createThumbnail(targetFile, 200, 200, thumbnail);
//                if(!thumbnailCreateed){
//                    org.apache.commons.io.FileUtils.copyFile(targetFile,thumbnail);
//
//                }
//
//            }else if(fileUploadTypeEnum==FileUploadTypeEnum.AVATAR|| fileUploadTypeEnum==FileUploadTypeEnum.ARTICLE_COVER){
//                ScaleFilter.createThumbnail(targetFile, 200, 200, targetFile);
//
//            }

dto.setLocalpath(localpath);
            dto.setOriginalFileName(originalFilename);
            return dto;

        } catch (BaseException e) {
            System.out.println(e+"沙漠化出家门");
            throw new BaseException("上传失败");


        } catch (IOException e) {
            System.out.println(e+"沙漠化出家门1");
            throw new RuntimeException(e);
        }
    }

}
